import PlatformUtil from "./Platforms/PlatformUtil";
import Controller from "./GamePlayController";
import XmlHttpTool from "./MyTools/XmlHttpTool";
import Toasts from "./Export/BaseToasts";
import { platform } from "os";

const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

  @property(cc.Node)
  GamePlayView: cc.Node = null;

  @property(cc.Node)
  GameStartView: cc.Node = null;

  @property(cc.Node)
  ContentImage: cc.Node = null;

  @property(cc.Label)
  TitleLabel: cc.Label = null;

  @property(cc.Label)
  DescLabel: cc.Label = null;

  @property(Controller)
  controller: Controller = null;

  @property
  UserCoinNum: number = 0;

  @property
  GameCoinNum: number = 0;

  @property(Toasts)
  toasts: Toasts = null;

  @property({type:cc.AudioClip})
  bgMusicSound = null;

  onLoad() {

    PlatformUtil.init();
    PlatformUtil.platform.init(this);
    // (<any>window).isHttpError=false
    this.lipsticksGetinfo();
    this.getUserCoin();
    cc.game.setFrameRate(80)

  }


  getUserCoin(){

    var that = this
    XmlHttpTool.instance.httpUtils("/users/coin", {}, function (successRes) {

      cc.log("获取用户金币数成功", successRes);
      that.UserCoinNum=successRes["coin"]
      
    }, function (faileRes) {
      cc.log("获取用户金币数失败", faileRes);
    }, function (errorRes) {
 
    });
  }


  lipsticksGetinfo() {


    var that = this
    XmlHttpTool.instance.httpUtils("/lipsticks/getinfo", { "trainid": PlatformUtil.platform.params["trainid"] }, function (successRes) {

      cc.log("娃娃详情列表", successRes);

      if((<any>window).isLoadSuccess==null){
        (<any>window).isLoadSuccess=true
        PlatformUtil.platform.loadSuccess()
        var isSwitchSound=PlatformUtil.platform.enableSound();
        (<any>window).isSound=isSwitchSound
        that.setGameBgmusiAndSound()
        
        cc.log("第一次初始化")
      }else{
        cc.log("第二次初始化")
      }
      that.GameCoinNum=successRes["cost"]
      that.TitleLabel.string = successRes["name"]
      var _this = that
      cc.loader.load({ url: successRes["thumbnail"], type: 'png' }, function (err, img) {

        var mylogo = new cc.SpriteFrame(img);
        _this.ContentImage.getComponent(cc.Sprite).spriteFrame = mylogo

      });


    }, function (faileRes) {
      cc.log("娃娃详情列表失败", faileRes);
      PlatformUtil.platform.loadFail()

    }, function (errorRes) {
      // PlatformUtil.platform.loadFail()
    });


  }
   
  setGameBgmusiAndSound(){

    var isSwitchSoundBg=PlatformUtil.platform.enableBGM();
    
    switch (cc.sys.os){
      case cc.sys.OS_ANDROID:
      if(isSwitchSoundBg){
         cc.audioEngine.play(this.bgMusicSound, true, 1);
      }
      break;
      case cc.sys.OS_IOS:
      PlatformUtil.platform.setBGM()
       cc.log("ios背景音效")
       break;

       default:
       break;

   } 
  }

  startGameClick() {
 
    if(this.UserCoinNum<this.GameCoinNum){
      this.toasts.add("娃娃币不足,请充值...");

      return
    }

    (<any>window).Gametype = 1

    var _this = this

    cc.director.loadScene("Watermelon", function () {

      _this.init(1)


    });

  }


  oneceAgame() {

    if(this.UserCoinNum<this.GameCoinNum){
      this.toasts.addToast("娃娃币不足,请充值...");
  
      return
    }

    var _this = this

    cc.director.loadScene("Watermelon", function () {

      _this.init(Gametype)


    });
  }

  init(gameTypeForm) {


    var GamePlayNode = cc.find("Canvas/Watermelon/GamePlayView")
    var GameStartNode = cc.find("Canvas/Watermelon/GameStartView")
    GameStartNode.active = false
    GamePlayNode.active = true
    GamePlayNode.getComponent("GamePlayController").startEnterGame(gameTypeForm)

  }


  tryClick() {

    (<any>window).Gametype = 2
    cc.log("试一试")
    var _this = this

    cc.director.loadScene("Watermelon", function () {

      _this.init(2)


    });

  }


  backAPPClick(){

    PlatformUtil.platform.exit()
  }


  checkBadyDatail() {

    cc.log("查看娃娃详情")

  }


  start() {

  }

  // update (dt) {}
}
